import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/users/login.vue'
import Home from './components/users/home.vue'
import Welcome from './components/users/welcome.vue'
import Users from './components/users/users.vue'
import Roles from './components/power/roles.vue'
import Rights from './components/power/rights.vue'
import Goods from './components/goods/list.vue'
import Params from './components/goods/params.vue'
import Categories from './components/goods/categories.vue'
import Orders from './components/order/orders.vue'
import Reports from './components/report/reports.vue'
import GoodsAdd from './components/goods/add.vue'
Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      redirect: '/welcome',
      component: Home,
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/roles', component: Roles },
        { path: '/rights', component: Rights },
        { path: '/goods', component: Goods },
        { path: '/params', component: Params },
        { path: '/categories', component: Categories },
        { path: '/orders', component: Orders },
        { path: '/reports', component: Reports },
        { path: '/goods/add', component: GoodsAdd }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next()  next('/login')
  if (to.path === '/login') return next()
  const tokenStr = sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router